#include "triggerconditionmodel.h"

TriggerConditionModel::TriggerConditionModel(AED_Trigger *trigger,QObject *parent) :
    QAbstractTableModel(parent),m_trigger(trigger)
{

}

int TriggerConditionModel::rowCount(const QModelIndex & parent) const{
    if(m_trigger)
        return m_trigger->GetConditions()->size();
    else
        return 0;

}

int TriggerConditionModel::columnCount(const QModelIndex & parent) const{
    return 2;
}

QVariant TriggerConditionModel::data ( const QModelIndex & index, int role) const{
    if(!m_trigger)
        return QVariant(QVariant::Invalid);
    if(role==Qt::DisplayRole||role==Qt::EditRole){
        int row = index.row();


        AED_Trigger::Condition cond = m_trigger->GetConditions()->operator [](row);

        QVariant var;

        if(index.column()==0)
            var = cond.type;
        else if(index.column()==1)
            var = cond.name;
        else
            return QVariant(QVariant::Invalid);
        return var;
    }
    return QVariant(QVariant::Invalid);
}

QVariant TriggerConditionModel::headerData ( int section, Qt::Orientation orientation, int role) const{

    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            if(section==0)
                return "Type";
            if(section==1)
                return "Name";
            else
                return QVariant(QVariant::Invalid);
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
    return QVariant(QVariant::Invalid);
}

bool TriggerConditionModel::setData(const QModelIndex &index, const QVariant &value, int role){
    if(!m_trigger)
        return false;

    int row = index.row();
    int column = index.column();

    if(index.isValid() && role==Qt::EditRole){
        if(column==0){
            m_trigger->GetConditions()->operator [](row).type = value.toString();
        }else if(column==1){
            m_trigger->GetConditions()->operator [](row).name = value.toString();
        }else{
            return false;
        }
        emit dataChanged(index,index);
        return true;
    }

    return false;
}

Qt::ItemFlags TriggerConditionModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags;

    flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;

    return flags;
}

void TriggerConditionModel::AddCondition(const QString &type, const QString &name){
    if(m_trigger)
        m_trigger->AddCondition(type,name);

    emit layoutChanged();
}

void TriggerConditionModel::RemoveCondition(int index){
    if(m_trigger)
        m_trigger->DeleteCondition(index);
    emit layoutChanged();
}

void TriggerConditionModel::Update(AED_Trigger *trigger){
    m_trigger = trigger;
    emit layoutChanged();
}

